"""
S4 Custom Prompts Input Node  
Author: S4MUEL
GitHub: https://github.com/S4MUEL-404/ComfyUI-S4API

Simple text input node with multiline support and formatting options.
"""

from ..s4_logger import S4APILogger, log_processing_step


class S4PromptsInput:
    """
    Simple text pass-through node with validation and formatting options.
    """

    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "text": (
                    "STRING", 
                    {
                        "default": "", 
                        "multiline": True,
                        "tooltip": "Text input for prompts"
                    }
                ),
            },
            "optional": {
                "trim_whitespace": (
                    "BOOLEAN",
                    {
                        "default": True,
                        "tooltip": "Remove leading and trailing whitespace"
                    }
                ),
                "remove_empty_lines": (
                    "BOOLEAN",
                    {
                        "default": False,
                        "tooltip": "Remove empty lines from multiline text"
                    }
                ),
            }
        }

    RETURN_TYPES = ("STRING",)
    RETURN_NAMES = ("text",)
    FUNCTION = "process_text"
    CATEGORY = "💀PromptsO"
    DESCRIPTION = "Simple text input node with formatting options for prompt processing."

    def process_text(self, text, trim_whitespace=True, remove_empty_lines=False):
        """Process and format input text"""
        
        log_processing_step("PromptsInput", "Starting text processing")
        
        if not text:
            S4APILogger.warning("PromptsInput", "Empty text input received")
            return ("",)
        
        original_length = len(text)
        processed_text = text
        
        # Apply formatting options
        if trim_whitespace:
            processed_text = processed_text.strip()
            S4APILogger.debug("PromptsInput", "Applied whitespace trimming")
        
        if remove_empty_lines and '\n' in processed_text:
            lines = processed_text.split('\n')
            non_empty_lines = [line for line in lines if line.strip()]
            processed_text = '\n'.join(non_empty_lines)
            S4APILogger.debug("PromptsInput", f"Removed empty lines: {len(lines)} → {len(non_empty_lines)} lines")
        
        final_length = len(processed_text)
        
        # Log results
        result_info = f"Length: {original_length} → {final_length} chars"
        if processed_text:
            lines = processed_text.split('\n')
            result_info += f", Lines: {len(lines)}"
            preview = processed_text[:50] + "..." if len(processed_text) > 50 else processed_text
            result_info += f", Preview: {preview}"
        
        S4APILogger.success("PromptsInput", f"Text processing completed - {result_info}")
        
        return (processed_text,)

    @classmethod
    def IS_CHANGED(cls, **kwargs):
        return float("NaN")